<?php
	include_once("./dal/CountryDAL.php");
	include_once("./dal/UserDAL.php");
	
	if(isLoggedIn())
	{
		header('Location: index.php');
	}
	
	$countries = getAllCountries();
	//print_r($countries);
	
	$regUsername = "";
	$regFirstName = "";
	$regLastName = "";
	$regGender = "";
	$regEmailAddress = "";
	$regPassword = "";
	$regConfirmPassword = "";
	$regStreet1 = "";
	$regStreet2 = "";
	$regCity = "";
	$regCountry = "";
	$regHomeContact = "";
	$regMobileContact = "";
	$regAboutMe = "";
	
	$fileUploadMessage = "";
	
	$formIsValid = true;
	
	$registrationSuccess = null;
	$profileImgUploadSuccess = null;
	
	if(isset($_POST["btnRegister"]))
	{
		if($_POST["btnRegister"]=="Register")
		{		
			if(isset($_POST["txtUsername"]))
			{
				$regUsername = trim($_POST["txtUsername"]);
			}
			
			if(isset($_POST["txtFirstName"]))
			{
				$regFirstName = trim($_POST["txtFirstName"]);
			}

			if(isset($_POST["txtLastName"]))
			{
				$regLastName = trim($_POST["txtLastName"]);
			}		

			if(isset($_POST["sex"]))
			{
				$regGender = trim($_POST["sex"]);
			}	

			if(isset($_POST["txtEmailAddress"]))
			{
				$regEmailAddress = trim($_POST["txtEmailAddress"]);
			}		

			if(isset($_POST["txtPassword"]))
			{
				$regPassword = trim($_POST["txtPassword"]);
			}	

			if(isset($_POST["txtStreet1"]))
			{
				$regStreet1 = trim($_POST["txtStreet1"]);
			}		

			if(isset($_POST["txtStreet2"]))
			{
				$regStreet2 = trim($_POST["txtStreet2"]);
			}	
			
			if(isset($_POST["txtCity"]))
			{
				$regCity = trim($_POST["txtCity"]);
			}	
			
			if(isset($_POST["ddlCountry"]))
			{
				$regCountry = trim($_POST["ddlCountry"]);
			}

			if(isset($_POST["txtHomeNumber"]))
			{
				$regHomeContact= trim($_POST["txtHomeNumber"]);
			}		

			if(isset($_POST["txtMobileNumber"]))
			{
				$regMobileContact= trim($_POST["txtMobileNumber"]);
			}		

			if(isset($_POST["txtAboutme"]))
			{
				$regAboutMe= trim($_POST["txtAboutme"]);
			}			
			
			$validFile = true;
			$newFileName = "";
			
			//if a file was uploaded
			if($_FILES['fileProfileImg']['name'])
			{
				//if no errors...
				if(!$_FILES['fileProfileImg']['error'])
				{
					//now is the time to modify the future file name and validate the file
					$tempFileName = strtolower($_FILES['fileProfileImg']['tmp_name']); //rename file
					$newFileName = $_FILES['fileProfileImg']['name'];
					$newFileName = $regUsername."_".$newFileName;
					
					if($_FILES['fileProfileImg']['size'] > (2048000)) //can't be larger than 2 MB
					{
						$validFile = false;
						$fileUploadMessage = 'Oops!  Your file\'s size is to large.';
					}
					
					//if the file has passed the test
					if($validFile)
					{
						//move it to where we want it to be
						$profileImgUploadSuccess = move_uploaded_file($_FILES['fileProfileImg']['tmp_name'], getcwd()."/img/profile/".$newFileName);
						
						$fileUploadMessage = 'Congratulations!  Your file was accepted.';
						if(!$profileImgUploadSuccess)
						{
							$formIsValid = false;
							$validFile = false;
						}
					}
				}
				//if there is an error...
				else
				{
					//set that to be the returned message
					$fileUploadMessage = 'Ooops!  Your upload triggered the following error:  '.$_FILES['fileProfileImg']['error'];
					$validFile = false;
				}
			}
			else
			{
				$validFile = false;
			}
			//print_r($countries);
						
			if($formIsValid)
			{
				$result = registerUser(
					$regUsername,
					$regFirstName,
					$regLastName,
					$regEmailAddress,
					$regPassword,
					$regAboutMe,
					$regGender,
					
					'',
					'',
					0,
					'',
					'',
					
					$regStreet1,
					$regStreet2,
					$regCountry,
					$regCity,
					'',
					
					'',
					'',
					$regHomeContact,
					
					'',
					'',
					$regMobileContact,
					2, //regular user
					$newFileName
				);
				
				$registrationSuccess = $result;
				
				if($registrationSuccess==true)
				{
					$_SESSION["username"] = $regUsername;
					
					//http_redirect("relpath", array("success" => "success"), true, HTTP_REDIRECT);
					
					header('Location: signup.php?success');
					exit;
				}
			}
		}
	}
	
	function getContent()
	{
		global 
		$countries,
		$regUsername,
		$regFirstName,
		$regLastName,
		$regGender,
		$regEmailAddress,
		$regPassword,
		$regConfirmPassword,
		$regStreet1,
		$regStreet2,
		$regCity,
		$regCountry,
		$regHomeContact,
		$regMobileContact,
		$regAboutMe,
		$formIsValid,
		$registrationSuccess,
		$profileImgUploadSuccess;
		
		if(isset($_REQUEST["success"]))
		{
			if(isset($_SESSION["username"]) && !empty($_SESSION["username"]))
			{
				include_once('./templates/RegistrationSuccess.php');
				unset($_SESSION["username"]);
			}
			else
			{
				include_once('./templates/RegistrationForm.php'); 
			}
		}
		else
		{
			include_once('./templates/RegistrationForm.php'); 
		}
	}
?>